function deleteList(){
    localStorage.clear();
    findAll();
}

function postaction(){
    var title = document.getElementById("title");
    if(title.value == "") {
        alert("请填写此字段");
    }else{
        var content = title.value;
        $.ajax({
            url: "/morning/todolist/create",
            type:"POST",
            context: document.body,
            data:"content="+content,
            success: function(result){
                var form=document.getElementById("form");
                form.reset();
                findAll();
            },
            error: function(){
            }
        });

    }
}

function ajaxRequest(url,data) {
    //var result;
    $.ajax({
        url: url,
        type:"POST",
        context: document.body,
        data:data,
        success: function(result){
            findAll();
        },
        error: function(){
            return;
        }
    });
    return;
}
/*

 */
function findAll() {
    //var result;
    $.ajax({
        url: "/morning/todolist/findAll",
        type:"POST",
        context: document.body,
        data:"userId=146",
        success: function(result){
            initTable(result);
        },
        error: function(){
            return;
        }
    });
    return;
}
/*
    get all todolist
 */
function initTable(result){
    var todolist=document.getElementById("todolist");
    var donelist=document.getElementById("donelist");

    if(result!=null){
        var data=result;
        var todoCount=0;
        var doneCount=0;
        var todoString="";
        var doneString="";
        for (var i = data.length - 1; i >= 0; i--) {
            if(data[i].flag == '1'){
                doneString+="<li draggable='true'><input type='checkbox' onchange='update("+data[i].id+",\"done\",false)' checked='checked' />"
                    +"<p id='p-"+data[i].id+"' onclick='edit("+data[i].id+")'>"+data[i].content+"</p>"
                    +"<a href='javascript:remove("+data[i].id+")'>-</a></li>";
                doneCount++;
            }
            else{
                todoString+="<li draggable='true'><input type='checkbox' onchange='update("+data[i].id+",\"done\",true)' />"
                    +"<p id='p-"+data[i].id+"' onclick='edit("+data[i].id+")'>"+data[i].content+"</p>"
                    +"<a href='javascript:remove("+data[i].id+")'>-</a></li>";
                todoCount++;
            }
        };
        todocount.innerHTML=todoCount;
        todolist.innerHTML=todoString;
        donecount.innerHTML=doneCount;
        donelist.innerHTML=doneString;
    }
    else{
        todocount.innerHTML=0;
        todolist.innerHTML="";
        donecount.innerHTML=0;
        donelist.innerHTML="";
    }

    var lis=todolist.querySelectorAll('ol li');
    [].forEach.call(lis, function(li) {
        li.addEventListener('dragstart', handleDragStart, false);
        li.addEventListener('dragover', handleDragOver, false);
        li.addEventListener('drop', handleDrop, false);

        /*onmouseout =function(){
            //saveSort();
        };*/
    });
}


function remove(i){
    var data;
    data = {"id":i}
    ajaxRequest("/morning/todolist/remove",data);
    load();
}

function update(i,field,value){
    var data;
    // request to background
    if(field == 'done'){
        data = {"id":i,"flag":value}
        ajaxRequest("/morning/todolist/finish",data);
    }else if(field == 'title'){
        data = {"id":i,"content":value}
        ajaxRequest("/morning/todolist/modify",data);
    }
}

function edit(i)
{

    var p = document.getElementById("p-"+i);
    title = p.innerHTML;
    p.innerHTML="<input id='input-"+i+"' value='"+title+"' />";
    var input = document.getElementById("input-"+i);
    input.setSelectionRange(0,input.value.length);
    input.focus();
    input.onblur =function(){
        if(input.value.length == 0){
            p.innerHTML = title;
            alert("O(∩_∩)O谢谢");
        }
        else{
            update(i,"title",input.value);
        }
    };
}

function load(){
    findAll();
}

window.onload=load;

window.addEventListener("storage",load,false);

var dragSrcEl = null;
function handleDragStart(e) {
    dragSrcEl = this;
    e.dataTransfer.effectAllowed = 'move';
    e.dataTransfer.setData('text/html', this.innerHTML);
}
function handleDragOver(e) {
    if (e.preventDefault) {
        e.preventDefault();
    }
    e.dataTransfer.dropEffect = 'move';
    return false;
}
function handleDrop(e) {
    if (e.stopPropagation) {
        e.stopPropagation();
    }
    if (dragSrcEl != this) {
        dragSrcEl.innerHTML = this.innerHTML;
        this.innerHTML = e.dataTransfer.getData('text/html');
    }
    return false;
}/**
 * Created by venn on 8/6/2017.
 */
